﻿using System;
using System.Collections.Generic;
using System.Text;
using WAD.WEB.AppCode;
using WAD.DAL;

namespace QD.Weijie.Web.Passport
{
    public partial class A_Renewal : PassportBase
    {
        protected string ExpTime1, ExpTime12, ExpTime3, GroupType, sitename, siteurl, linkman, phone, date, endtime, groupname;
        protected StringBuilder sbGroup = new StringBuilder();
        protected StringBuilder sbPrice = new StringBuilder();

        protected float discount;
        protected int id, groupid;
        protected Decimal balance;

        protected void Page_Load(object sender, EventArgs e)    
        {
            if (!IsAgent())
                Echo("您不是代理商", "index.aspx");

            BindData();
        }
        void BindData()
        {
            id = Parse.Int("id", 0);
            if (id < 1)
            {
                Echo("参数无效", "a_list.aspx");
                return;
            }

            //代理商判断
            Dictionary<string, object> agent = GetAgent();//代理商
            if (agent == null)
            {
                Echo("您不是代理商", "index.aspx");
                return;
            }
            Dictionary<string, object> user = DaoUser.GetDetail(id, "*");    //终端客户

            if (user == null || agent == null || user["agentid"] == DBNull.Value || Convert.ToInt32(user["agentid"]) != user_id)
            {
                Echo("参数无效", "a_list.aspx");
                return;
            }

            //刷新代理商余额
            agent["balance"] = WAD.DAL.Union.DAOAgent.RefrushBalance(agent["balance"], Convert.ToInt32(agent["userid"]));

            groupname = "免费会员";
            discount = Convert.ToSingle(agent["discount"]) / 10;   //折扣
            sitename = Convert.ToString(user["nick_name"]);
            phone = Convert.ToString(user["phone"]);
            balance = 0;
            date = "";
            endtime = date;
            balance = Convert.ToDecimal(agent["balance"]);  //代理商余额

            if (user["date"] != null)
                date = Convert.ToDateTime(user["date"]).ToString("yyyy-MM-dd");

            BindVersion();      //系统版本
            BindUserVersion();  //用户版本
        }
     
        Dictionary<string, object> GetAgent()
        {
            return WAD.DAL.Union.DAOAgent.Get(user_id);
        }
        
        void BindVersion()
        {
            sbGroup.Clear();
            List<Dictionary<string, object>> list = WAD.DAL.Union.DAOAgent.GetList("select top 10 GroupID,GroupTitle from sql_s002.dbo.vIFO_Users_Groups where GroupType=12");
            for (int i = 0; i < list.Count; i++)
                sbGroup.AppendFormat("<option value='{0}' ref='{1}'>{2}</option>", list[i]["GroupID"], "", list[i]["GroupTitle"]);
        }
        void BindUserVersion()
        {
            Dictionary<string, object> oldversion = DAO.Get("user_version", "user_id=" + id, "end_date,version_id");
            if (oldversion != null)
            {
                endtime = Convert.ToDateTime(oldversion["end_date"]).ToString("yyyy-MM-dd");
                groupname = GetVersion(Convert.ToInt32(oldversion["version_id"]));
            }
        }


        Dictionary<string, object> GetAgent_old()
        {
            return DAO.Get("agent", "userid=" + user_id, "*");
        }
        void BindVersion_old()
        {
            sbGroup.Clear();
            List<Dictionary<string, object>> list = DAOVersion.GetList();
            for (int i = 0; i < list.Count; i++)
                sbGroup.AppendFormat("<option value='{0}' ref='{1}'>{2}</option>", list[i]["id"], "", list[i]["name"]);
        }
    }
}